<template>
  <div>
    <!-- 表格 -->
    <div>
      <el-table :data="goodsList" style="width:100%" stripe ref="multipleTable"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="商品图片" align="center">
          <template slot-scope="scope">
            <l-pic v-for="(item,index) in scope.row.pic_urls.split(',').filter(p => p !== '')" :key="index" 
              :picurl="item" :size="{width:40,height:40}" 
              :disdel="false" :disview="false"/>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" align="center"></el-table-column>
        <el-table-column prop="price" label="价格" align="center"></el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import { getReGoodsList, postReGoods } from '@/api/marketing'
import LPic from '@/components/lPic'

export default {
  components: {
    LPic
  },
  data() {
    return {
      page: 1,
      count: 1,

      goodsList: [],

      selectionList: [],
    }
  },
  mounted() {
    this.getReGoodsList();
  },
  methods: {
    /**
     * 获取商品列表
     */
    getReGoodsList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        page: this.page,
        limit: 10
      };
      getReGoodsList(params).then(res => {
        if(res.status === 200){
          this.goodsList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.goodsList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 提交
     */
    submit() {
      let selectId = [];
      this.selectionList.forEach(item => {
        selectId.push(item.id);
      })
      postReGoods({key: this.$store.state.app.activeApp.saa_key, goods_ids: selectId}).then(response => {
        if(response.status === 200){
          this.$emit('success');
          this.$message.success('添加成功！');
        }else{
          this.$message.error(response.message);
        }
      })
        
    },
    /**
     * 选择
     */
    handleSelectionChange(val) {
      this.selectionList = val;
      // this.multipleSelection = val;
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getReGoodsList();
    },
  },
}
</script>